package com.hsy.crm.web.utils;

import com.hsy.crm.web.domain.entity.Order;
import com.hsy.crm.web.domain.entity.OrderDetail;
import com.hsy.crm.web.domain.vo.order.OrderDetailVo;

import java.util.List;

public class OrderUtils {

    public static void divPurPrice(List<OrderDetailVo> orderDetails, Order dbOrder){
        if(dbOrder.getPurPrice().intValue() == 0){
            setDefaultPurPercentPrice(orderDetails);
        }else if( orderDetails.size() == 1 ){
            orderDetails.get(0).setPurPercentPrice(dbOrder.getPurPrice());
        }else{
            //总比例
            double percent = CalcUtils.div(dbOrder.getPurPrice(),dbOrder.getDueMoney());

            Double[] shares = new Double[orderDetails.size()];

            for( int i = 0 ,size = orderDetails.size(); i<size ; i++ ){
                OrderDetail orderDetail = orderDetails.get(i);
                //每行商品折后金额 * 总比例 = 平摊金额
                Double share = CalcUtils.mul(percent,orderDetail.getDiscountPrice());
                shares[i] = share;

                if( i == size-1 ){
                    double number = addDecimals(shares);
                    Long percentPrice = Math.round(number)+share.intValue();
                    orderDetail.setPurPercentPrice(percentPrice);
                }else{
                    orderDetail.setPurPercentPrice(share.longValue());
                }
            }
        }
    }

    public static void divDiscountMoney(List<OrderDetailVo> orderDetails, Order dbOrder){
        if(dbOrder.getDiscountMoney().intValue() == 0){
            setDefaultPercentPrice(orderDetails);
        }else if( orderDetails.size() == 1 ){
            orderDetails.get(0).setPercentPrice(dbOrder.getDiscountMoney());
        }else{
            //总比例
            double percent = CalcUtils.div(dbOrder.getDiscountMoney(),dbOrder.getDueMoney());

            Double[] shares = new Double[orderDetails.size()];

            for( int i = 0 ,size = orderDetails.size(); i<size ; i++ ){
                OrderDetail orderDetail = orderDetails.get(i);
                //每行商品折后金额 * 总比例 = 平摊金额
                Double share = CalcUtils.mul(percent,orderDetail.getDiscountPrice());
                shares[i] = share;

                if( i == size-1 ){
                    double number = addDecimals(shares);
                    Long percentPrice = Math.round(number)+share.intValue();
                    orderDetail.setPercentPrice(percentPrice);
                }else{
                    orderDetail.setPercentPrice(share.longValue());
                }
            }
        }
    }

    private static void setDefaultPercentPrice(List<OrderDetailVo> orderDetails){
        for(OrderDetail orderDetail : orderDetails){
            orderDetail.setPercentPrice(0L);
        }
    }

    private static void setDefaultPurPercentPrice(List<OrderDetailVo> orderDetails){
        for(OrderDetail orderDetail : orderDetails){
            orderDetail.setPurPercentPrice(0L);
        }
    }

    /**
     * 所有小数部分相加
     * @param shares
     * @return
     */
    private static double addDecimals(Double[] shares){
        double result = 0;
        for (double number : shares){
            result = CalcUtils.add(result,CalcUtils.getDecimal(number));
        }
        return result;
    }

}
